package com.jjwxc.jwjskandriod.widget;

import static com.jjwxc.jwjskandriod.framework.base.FFApplication.showToast;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.jjwxc.jwjskandriod.R;
import com.jjwxc.jwjskandriod.framework.utils.FFUtils;
import com.jjwxc.jwjskandriod.framework.utils.PreUtils;
import com.jjwxc.jwjskandriod.framework.utils.StUtils;

public class EnterPasswordDialog extends Dialog implements
        View.OnClickListener {
    private Context context;
    private TextView dialog_ok;
    private TextView dialog_no;

    private ZSerialnumberLayout gjyserial_one;
    private ZSerialnumberLayout gjyserial_two;
    private String code_one;
    private String code_two;
    private DialogClickListener listener;

    public EnterPasswordDialog(Context context) {
        super(context);
        this.context = context;
    }

    public EnterPasswordDialog(Context context,
                               DialogClickListener listener) {
        super(context, R.style.my_dialog);
        this.context = context;


        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.dialog_enter_password);

        gjyserial_one = findViewById(R.id.gjyserial_one);
        gjyserial_two = findViewById(R.id.gjyserial_two);
        dialog_ok = findViewById(R.id.tv_ok);
        dialog_no = findViewById(R.id.tv_queding);
        dialog_ok.setOnClickListener(this);
        dialog_no.setOnClickListener(this);

        initView();
        initDialog(context);
    }

    /**
     * 设置dialog的宽为屏幕的3分之1
     *
     * @param context
     */
    private void initDialog(Context context) {
        setCanceledOnTouchOutside(false);
        setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface dialog, int keyCode,
                                 KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK
                        && event.getRepeatCount() == 0) {
                    return true;
                } else {
                    return false;
                }
            }
        });
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.width = (FFUtils.getDisWidth() / 5 * 4); // 设置宽度
        this.getWindow().setAttributes(lp);
    }

    private void initView() {
        gjyserial_one.setOnInputListener(new ZSerialnumberLayout.OnInputListener() {
            @Override
            public void onSucess(String code) {
                code_one = code;
            }
        });
        gjyserial_two.setOnInputListener(new ZSerialnumberLayout.OnInputListener() {
            @Override
            public void onSucess(String code) {
                code_two = code;
            }
        });
    }

    @Override
    public void onClick(View v) {
        dismiss();
        if (listener == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.tv_ok:
                if (!StUtils.isEmpty(code_one) && !StUtils.isEmpty(code_two) && code_one.equals(code_two)) {
                    PreUtils.setString("QING_password", code_one);
                    listener.onOkClick(this);
                }else{
                    showToast("监护密码不正确");
                }
                break;
            case R.id.tv_queding:
                listener.onNoClick(this);
                break;
            default:
                break;
        }
    }

    public interface DialogClickListener {
        void onOkClick(Dialog dialog);

        void onNoClick(Dialog dialog);

    }
}
